package com.banciyuan.bcywebview.biz.follow;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.biz.main.mineinfo.AddFriendsActivity;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;

/**
 * Created by hy on 2015/4/28.
 */
public class FollowingActivity extends BaseActivity {
    private String uid;
    private String type = "following";
    private String title = "";
    private boolean removeFansFlag = false;
    private TextView tvRemoveFans;
    private FragmentManager fragmentManager;
    private FollowFragment mFollowFragment;
    private ActionbarHelper mActionbarHelper;
    private View mActionBarView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container);
        initArgs();
        initActionbar();
        getFragment();
    }

    private void getFragment() {
        fragmentManager = getSupportFragmentManager();
        mFollowFragment = new FollowFragment();
        Bundle bundle = new Bundle();
        bundle.putString(gotoUtil.INTENT_VALUE_ONE, uid);
        bundle.putString(gotoUtil.INTENT_VALUE_TWO, type);
        bundle.putBoolean("preLoad", true);
        mFollowFragment.setArguments(bundle);
        if (!mFollowFragment.isAdded()) {
            FragmentTransaction fts = fragmentManager.beginTransaction();
            fts.add(R.id.fragment_container, mFollowFragment, "mFollowFragment");
            fts.commit();
        }
    }

    @Override
    protected void initArgs() {
        uid = StringUtil.getString(getIntent(), gotoUtil.INTENT_VALUE_ONE);
        type = StringUtil.getString(getIntent(), gotoUtil.INTENT_VALUE_TWO);
        if (TextUtils.isEmpty(type)) {
            type = "following";
        }
        title = StringUtil.getString(getIntent(), gotoUtil.INTENT_VALUE_THREE);
    }

    @Override
    protected void initActionbar() {
        mActionBarView = findViewById(R.id.base_action_bar);
        mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(title);
        initRemoveFans();
    }

    private void initRemoveFans() {
        tvRemoveFans = (TextView) findViewById(R.id.base_action_bar_home_text_item);
        tvRemoveFans.setTextSize(12);
        tvRemoveFans.setTextColor(getResources().getColor(R.color.mine_textcolor));
        if (StringUtil.notNullEqual(type, "follower") && StringUtil.notNullEqual(uid, UserDataHelper.getInstance(this).getUid())) {
            tvRemoveFans.setVisibility(View.VISIBLE);
            tvRemoveFans.setText(getString(R.string.remove_fans));
            tvRemoveFans.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    removeFansFlag = !removeFansFlag;
                    tvRemoveFans.setText(removeFansFlag ? getString(R.string.cancel_clear) : getString(R.string.remove_fans));
                    if (null != mFollowFragment)
                        mFollowFragment.doRemoveFans(removeFansFlag);
                }
            });
        } else if (StringUtil.notNullEqual(type, "following") && StringUtil.notNullEqual(uid, UserDataHelper.getInstance(this).getUid())) {
            tvRemoveFans.setVisibility(View.VISIBLE);
            tvRemoveFans.setText(getString(R.string.add_friends));
            tvRemoveFans.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    gotoUtil.gotoAct(FollowingActivity.this, AddFriendsActivity.class);
                }
            });
        } else {
            tvRemoveFans.setVisibility(View.GONE);
        }
    }

}
